/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIPizzaEnvenenada.java
 *
 * Created on 28/11/2010, 02:20:45 PM
 */

package PizzaEnvenenada;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;

import javax.swing.JOptionPane;

/**
 *
 * @author jefferpc
 */
public class GUIPizzaEnvenenada extends javax.swing.JFrame implements ActionListener{

    /** Creates new form GUIPizzaEnvenenada */
    ArbolMinMaxPizza arbolMinMax;
    boolean orden;
    int porciones;
    String ganador;
    boolean termino;
    int respuesta;
    public GUIPizzaEnvenenada(boolean orden) {
        initComponents();
        this.orden=orden;
        //JOptionPane.showMessageDialog(this, "El Juego consiste en :\nhola");
        inic();
        setVisible(true);
        if(orden)
            jugar();
    }
    public void  jugar()
    {
            jbComer1.setEnabled(false);
            jbComer2.setEnabled(false);
            jbComer3.setEnabled(false);
            arbolMinMax.inicializar(porciones);
            int x=Integer.parseInt(arbolMinMax.tomarDecision());
            
            int y=x;
            x=(porciones-x);
            porciones=y;
            
            
         
            jlUltimaJugada.setText("Donatelo se comio "+x+" porciones!!");
            
        
            jtHistorial.append("Donatelo>> "+x+"\n" );
            
            verificarGanador("TU");
            habilitar();

    }

    public int getRespuesta() {
        return respuesta;
    }

    

    public void habilitar()
    {
        if(!termino)
        {
        if(porciones>0)
            jbComer1.setEnabled(true);
        if(porciones>1)
            jbComer2.setEnabled(true);
        if(porciones>2)
            jbComer3.setEnabled(true);
        }
    }
    public void verificarGanador(String ganador)
    {
        jlCantPorciones.setText("Quedan "+porciones+" porciones de pizza!!");
        if(porciones==0)
        {
            jbComer1.setEnabled(false);
            jbComer2.setEnabled(false);
            jbComer3.setEnabled(false);
            jbAceptar.setEnabled(true);
            termino=true;
            
            
            if(ganador.equals("Donatelo"))
            {
                jlResultado.setText("Perdiste!!!!!");
                jlImagenResultado.setIcon(new ImageIcon("perder.jpg"));
                respuesta=2;
            }
            else
            {
                 jlResultado.setText("Ganaste!!!!!");
                 jlImagenResultado.setIcon(new ImageIcon("ganar.jpg"));
                respuesta=1;
            }//JOptionPane.showMessageDialog(null, "Gano "+ganador);

        }
    }
    public void inic()
    {
        respuesta=-1;
        termino=false;
        jbAceptar.setEnabled(false);
        ganador="";
        arbolMinMax=new ArbolMinMaxPizza();
        
        porciones=8;
        jlCantPorciones.setForeground(Color.RED);
        panelJugar.setBackground(Color.BLACK);
        jbAceptar.addActionListener(this);
        jbComer1.addActionListener(this);
        jbComer2.addActionListener(this);
        jbComer3.addActionListener(this);
        jbAceptar.addActionListener(this);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelJugar = new javax.swing.JLayeredPane();
        jlCantPorciones = new javax.swing.JLabel();
        jlImagenPizza = new javax.swing.JLabel();
        jlUltimaJugada = new javax.swing.JLabel();
        jbComer1 = new javax.swing.JButton();
        jbComer2 = new javax.swing.JButton();
        jbComer3 = new javax.swing.JButton();
        otroPanel = new javax.swing.JLayeredPane();
        jbAceptar = new javax.swing.JButton();
        jlImagenResultado = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtHistorial = new javax.swing.JTextArea();
        jlHistorial = new javax.swing.JLabel();
        jlResultado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(24, 134, 244));

        panelJugar.setBackground(new java.awt.Color(40, 102, 244));
        panelJugar.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        jlCantPorciones.setText("Quedan 8 Porciones de Pizza");
        jlCantPorciones.setBounds(40, 30, 200, 17);
        panelJugar.add(jlCantPorciones, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jlImagenPizza.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PizzaEnvenenada/PepperoniPizza-full.jpg"))); // NOI18N
        jlImagenPizza.setBounds(40, 50, 200, 200);
        panelJugar.add(jlImagenPizza, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jlUltimaJugada.setBounds(40, 260, 240, 20);
        panelJugar.add(jlUltimaJugada, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jbComer1.setText("Comer 1");
        jbComer1.setToolTipText("Comer 1 porcion de pizza");
        jbComer1.setBounds(270, 50, 100, 29);
        panelJugar.add(jbComer1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jbComer2.setText("Comer 2");
        jbComer2.setToolTipText("Comer 2 porcion de pizza");
        jbComer2.setBounds(270, 130, 100, 29);
        panelJugar.add(jbComer2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jbComer3.setText("Comer 3");
        jbComer3.setToolTipText("Comer 3 porcion de pizza");
        jbComer3.setBounds(270, 210, 100, 29);
        panelJugar.add(jbComer3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        otroPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        jbAceptar.setText("Aceptar");
        jbAceptar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.setBounds(10, 290, 130, 29);
        otroPanel.add(jbAceptar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jlImagenResultado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PizzaEnvenenada/interrogacion.gif"))); // NOI18N
        jlImagenResultado.setBounds(20, 200, 110, 80);
        otroPanel.add(jlImagenResultado, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jtHistorial.setColumns(8);
        jtHistorial.setEditable(false);
        jtHistorial.setRows(5);
        jScrollPane1.setViewportView(jtHistorial);

        jScrollPane1.setBounds(10, 20, 130, 150);
        otroPanel.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jlHistorial.setText("Historial");
        jlHistorial.setBounds(10, 0, 80, 17);
        otroPanel.add(jlHistorial, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jlResultado.setText("Ganaras?");
        jlResultado.setBounds(10, 180, 130, 17);
        otroPanel.add(jlResultado, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelJugar, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(otroPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelJugar, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)
                    .addComponent(otroPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jbAceptarActionPerformed

    /**
    * @param args the command line arguments
    */
   public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIPizzaEnvenenada(false).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbComer1;
    private javax.swing.JButton jbComer2;
    private javax.swing.JButton jbComer3;
    private javax.swing.JLabel jlCantPorciones;
    private javax.swing.JLabel jlHistorial;
    private javax.swing.JLabel jlImagenPizza;
    private javax.swing.JLabel jlImagenResultado;
    private javax.swing.JLabel jlResultado;
    private javax.swing.JLabel jlUltimaJugada;
    private javax.swing.JTextArea jtHistorial;
    private javax.swing.JLayeredPane otroPanel;
    private javax.swing.JLayeredPane panelJugar;
    // End of variables declaration//GEN-END:variables

    public void actionPerformed(ActionEvent ae) {
        //throw new UnsupportedOperationException("Not supported yet.");
        Object x=ae.getSource();
        if(x==jbComer1)
        {
            porciones=porciones-1;
            jtHistorial.append("Jugador>> "+1+"\n" );
            verificarGanador("Donatelo");

            System.out.println("J>> "+porciones);
            if(!termino)
            jugar();
        }
        if(x==jbComer2)
        {
            porciones=porciones-2;
            jtHistorial.append("Jugador>> "+2+"\n" );
            verificarGanador("Donatelo");

            System.out.println("J>> "+porciones);
            if(!termino)
            jugar();
        }
        if(x==jbComer3)
        {
            porciones=porciones-3;
            jtHistorial.append("Jugador>> "+3+"\n" );
            verificarGanador("Donatelo");
            System.out.println("J>> "+porciones);
            if(!termino)
            jugar();
        }
    }

    
}
